package com.yunhaikeji.api.common;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class AudioUtils {

    /**
     * wav和pcm的区别就是wav在pcm的前面多了44字节
     * WAV转PCM文件
     * @param wavFilePath wav文件路径
     */
    public static void wavToPcm(String wavFilePath){
        try {
            byte[] preBuffer= new byte[44];
            byte[] buffer= new byte[1024];
            FileInputStream fis = new FileInputStream(wavFilePath);
            String pcmFilePath = wavFilePath.substring(0,wavFilePath.lastIndexOf(".")+1)+"pcm";
            FileOutputStream fos = new FileOutputStream(pcmFilePath);
            //剔除44位的wav前缀
            if (fis.read(preBuffer)!=-1) {
                int readByte = 0;
                //复制pcm内容
                while((readByte = fis.read(buffer)) != -1) {
                    fos.write(buffer,0, readByte);
                }
                fos.flush();
                fos.close();
                fis.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {

        String wavFile = "D:\\IdeaProjects\\monitor\\api\\src\\main\\resources\\static\\audio\\test.wav";
        wavToPcm(wavFile);

    }

}
